package org.hhgzs.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.hhgzs.modules.erp.constants.BusinessConstants;
import org.hhgzs.modules.erp.entity.Account;
import org.hhgzs.modules.erp.entity.Depot;
import org.hhgzs.modules.erp.mapper.AccountMapper;
import org.hhgzs.modules.erp.mapper.DepotMapper;
import org.hhgzs.modules.erp.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hhgzs.modules.erp.service.DepotService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 账户信息 服务实现类
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@Service
@RequiredArgsConstructor
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    private final DepotService depotService;
    private final AccountMapper accountMapper;

    @Override
    public int updateIsDefault(Long depotId) {
        Account data = new Account();
        UpdateWrapper<Account> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Account::getId,depotId);
        updateWrapper.lambda().set(Account::getIsDefault,true);
        this.update(updateWrapper);

        updateWrapper.clear();

        updateWrapper.lambda().ne(Account::getId,depotId);
        updateWrapper.lambda().set(Account::getIsDefault,false);
        this.update(updateWrapper);

        return 1;
    }

    @Override
    public Account getAccount(long id) throws Exception{
        return getById(id);
    }


    @Override
    public List<Account> getAccount(){

        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().ne(Account::getDeleteFlag, BusinessConstants.DELETE_FLAG_DELETED);
        queryWrapper.lambda().orderBy(true, true, Account::getId);
        List<Account> list = list(queryWrapper);
        return list;


    }
}
